package DEEPERsource.DEEPERsource.source.machinelearning;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;
import java.util.Map;

import machinelearning.wekawrapper.WekaClassifier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weka.classifiers.trees.J48;
@Deprecated
public class DecisionTreeClassifier extends WekaClassifier {
	private static Log _log = LogFactory.getLog(DecisionTreeClassifier.class);
	
	public DecisionTreeClassifier(){
		classifier = new J48();
	}

	@Override
	public void buildClassifier() {
		try {
			classifier.buildClassifier(trainingSet);
		} catch (Exception e) {
			_log.error("Error building classifier");
			e.printStackTrace();
		}
	}

	@Override
	public void readClassifier(String modelFile) {
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(modelFile));
			classifier  = (J48)in.readObject();
		} catch (FileNotFoundException e) {
			_log.error("Can't read the file");
			e.printStackTrace();
		} catch (IOException e) {
			_log.error("File read error");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			_log.error("Error reading classifier");
			e.printStackTrace();
		}
	}

	@Override
	public Map<String, String> getOptions() {
		Map<String, String> options = new HashMap<String,String>();
		options.put("confidenceFactor",Float.toString(((J48)classifier).getConfidenceFactor()));
		return options;
	}

	@Override
	public void setOptions(Map<String, String> opt) {
		((J48)classifier).setConfidenceFactor(Float.parseFloat(opt.get("confidenceFactior")));		
	}
}
